package org.linlinjava.litemall.brand.web;

import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import org.linlinjava.litemall.brand.annotation.LoginUser;
import org.linlinjava.litemall.core.util.JacksonUtil;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.AutoReply;
import org.linlinjava.litemall.db.service.AutoReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description:
 * @program: mall
 * @author: youvy
 * @create: 2019-12-14 10:10
 **/
@RestController
@RequestMapping("/autoReply")
public class AutoReplyController {
	@Autowired
	private AutoReplyService autoReplyService;
	@PostMapping("/getReplyByType")
	public Object getReplyByType(@RequestBody String json, HttpServletRequest request){
		XxlSsoUser user = (XxlSsoUser) (request.getAttribute(Conf.SSO_USER));
		Integer imId = Integer.parseInt(user.getPlugininfo().get("imId"));
		Integer type = JacksonUtil.parseInteger(json, "type");
		List<AutoReply> autoReplies = autoReplyService.getReplyByType(imId, type);
		return ResponseUtil.ok(autoReplies);
	}
	@PostMapping("/insert")
	public Object insert(@RequestBody AutoReply autoReply,HttpServletRequest request){
		XxlSsoUser user = (XxlSsoUser) (request.getAttribute(Conf.SSO_USER));
		Integer imId = Integer.parseInt(user.getPlugininfo().get("imId"));
		autoReply.setImId(imId);
		autoReplyService.insert(autoReply);
		return ResponseUtil.ok();
	}

	@PostMapping("/update")
	public Object update(@RequestBody AutoReply autoReply){
		autoReplyService.update(autoReply);
		return ResponseUtil.ok();
	}

	@GetMapping("/delete")
	public Object delete(int id){
		autoReplyService.delete(id);
		return ResponseUtil.ok();
	}

}
